package com.hlw.example.selvlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/AddTextBookServlet")
public class AddTextBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取表单数据
        String bookId = request.getParameter("bookId");
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String price = request.getParameter("price");
        String isNational = request.getParameter("isNational");
        String category = request.getParameter("category");
        String remark = request.getParameter("remark");

        // 使用 getWriter() 输出收集到的信息
        PrintWriter out = response.getWriter();
        out.println("教材编号：" + bookId + "<br>");
        out.println("教材名：" + bookName + "<br>");
        out.println("作者：" + author + "<br>");
        out.println("价格：" + price + "<br>");
        out.println("是否国规教材：" + isNational + "<br>");
        out.println("教材类别：" + category + "<br>");
        out.println("备注：" + remark + "<br>");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}



//package com.hlw.example.selvlet;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//
//@WebServlet("/AddTextBookServlet")
//public class AddTextBookServlet extends HttpServlet {
//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        // 处理中文乱码
//        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=UTF-8");
//
//        String bookId = request.getParameter("bookId");
//        String bookName = request.getParameter("bookName");
//        String author = request.getParameter("author");
//        String price = request.getParameter("price");
//        String isNational = request.getParameter("isNational");
//        String category = request.getParameter("category");
//        String remark = request.getParameter("remark");
//
//        PrintWriter out = response.getWriter();
//        out.println("教材编号：" + bookId + "<br>");
//        out.println("教材名：" + bookName + "<br>");
//        out.println("作者：" + author + "<br>");
//        out.println("价格：" + price + "<br>");
//        out.println("是否国规教材：" + isNational + "<br>");
//        out.println("教材类别：" + category + "<br>");
//        out.println("备注：" + remark + "<br>");
//    }
//
//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        doPost(request, response);
//    }
//}
